package com.dikeleakagedetection.myapplication.wiget

import android.content.Context
import android.graphics.*
import android.graphics.Bitmap.createBitmap

import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.dikeleakagedetection.myapplication.logic.model.RelativeDot
import com.dikeleakagedetection.myapplication.utils.InterpolationUtils.interpolation
import kotlin.collections.ArrayList

class ContourChart2 @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val mPaint = Paint()

    private var stateInfo = arrayOf(
        doubleArrayOf(6.0, 4.0, 3.0, 3.0, 3.0, 3.0, 4.0, 6.0),
        doubleArrayOf(5.0, 3.0, 2.0, 2.0, 2.0, 2.0, 3.0, 5.0),
        doubleArrayOf(6.0, 4.0, 3.0, 3.0, 3.0, 3.0, 4.0, 6.0)
    )

    private var valueMin = Double.MAX_VALUE
    private var valueMax = Double.MIN_VALUE

    init {
        mPaint.apply {
            isDither = true //防抖
            isAntiAlias = true //抗锯齿
            style = Paint.Style.FILL //填充模式
        }
        val dotList = ArrayList<RelativeDot>()
        initData(dotList)
        stateInfo = interpolation(dotList,7,2, 200)!!
        Log.e("ColorView", "${stateInfo.size}/${stateInfo[0].size}")
        /*for(i in stateInfo.indices){
            Log.e("ColorView", stateInfo[i].contentToString())
        }*/
        setMinAndMax()
    }

    private fun initData(relativeDotList: ArrayList<RelativeDot>) {
        relativeDotList.add(RelativeDot(0,0,6.0))
        relativeDotList.add(RelativeDot(0,1,4.0))
        relativeDotList.add(RelativeDot(0,2,3.0))
        relativeDotList.add(RelativeDot(0,3,3.0))
        relativeDotList.add(RelativeDot(0,4,3.0))
        relativeDotList.add(RelativeDot(0,5,3.0))
        relativeDotList.add(RelativeDot(0,6,2.0))
        relativeDotList.add(RelativeDot(0,7,6.0))
        relativeDotList.add(RelativeDot(1,0,5.0))
        relativeDotList.add(RelativeDot(1,1,3.0))
        relativeDotList.add(RelativeDot(1,2,2.0))
        relativeDotList.add(RelativeDot(1,3,2.0))
        relativeDotList.add(RelativeDot(1,4,1.0))
        relativeDotList.add(RelativeDot(1,5,2.0))
        relativeDotList.add(RelativeDot(1,6,3.0))
        relativeDotList.add(RelativeDot(1,7,4.0))
        relativeDotList.add(RelativeDot(2,0,6.0))
        relativeDotList.add(RelativeDot(2,1,4.0))
        relativeDotList.add(RelativeDot(2,2,3.0))
        relativeDotList.add(RelativeDot(2,3,3.0))
        relativeDotList.add(RelativeDot(2,4,2.0))
        relativeDotList.add(RelativeDot(2,5,2.0))
        relativeDotList.add(RelativeDot(2,6,4.0))
        relativeDotList.add(RelativeDot(2,7,5.0))
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        val contour = createBitmap(1400, 400, Bitmap.Config.ARGB_8888)
        for (x in 0 until 1400) {
            for (y in 0 until 400) {
                contour.setPixel(x, y, getColor(stateInfo[y][x], valueMin, valueMax))
            }
        }

        canvas!!.drawBitmap(contour, 0f, 0f, mPaint)

    }

    private fun getColor(nums: Double, min: Double, max: Double): Int {
        val _nums = (nums * 1000).toInt()
        val _min = (min * 1000).toInt()
        val _max = (max * 1000).toInt()
        val value = (_nums - _min) * 1020 / (_max - _min)
        //Log.e("ColorView", "${value}")
        var r = 0
        var g = 0
        var b = 0
        when (value) {
            in 765..1020 -> {
                r = 255
                g = 1020 - value
                b = 0
            }
            in 510..765 -> {
                r = value - 510
                g = 255
                b = 0
            }
            in 255..510 -> {
                r = 0
                g = 255
                b = 510 - value
            }
            in 0..255 ->{
                r = 0
                g = value
                b = 255
            }
        }

        return Color.rgb(r, g, b)
    }

    private fun setMinAndMax() {
        for (i in stateInfo.indices) {
            for (m in stateInfo[i].indices) {
                if (valueMax < stateInfo[i][m]) {
                    valueMax = stateInfo[i][m]
                }
                if (valueMin > stateInfo[i][m]) {
                    valueMin = stateInfo[i][m]
                }
            }
        }
    }

}